package indi.jp.project.business.jingying.controller;

import indi.jp.project.business.jingying.pojo.dto.BidsDTO;
import indi.jp.project.business.jingying.pojo.dto.BidsResultDTO;
import indi.jp.project.business.jingying.pojo.query.BidsQuery;
import indi.jp.project.business.jingying.service.BidsService;
import indi.jp.project.core.config.PropertyConfig;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.entity.po.AttachmentPO;
import indi.jp.project.core.util.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.File;

/**
 * 投标Controller
 *
 * @author jiangping
 * @date 2022/5/13
 */
@RestController
@RequestMapping("/jingying/bids")
public class BidsController {

    @Resource
    private PropertyConfig propertyConfig;

    @Resource
    private BidsService bidsService;

    @GetMapping("/grid")
    public JsonResult queryGrid(BidsQuery query) throws Exception {
        return bidsService.queryGrid(query);
    }

    @PostMapping("/save")
    public JsonResult save(@RequestBody @Valid BidsDTO bidsDTO) throws Exception {
        return bidsService.save(bidsDTO);
    }

    @GetMapping("/find")
    public JsonResult findById(@RequestParam @NotBlank String id, @RequestParam @NotBlank String type) throws Exception {
        return bidsService.findVoById(id, type);
    }

    @PostMapping("/delete")
    public JsonResult deleteById(@RequestParam @NotBlank String id) throws Exception {
        return bidsService.deleteById(id);
    }

    @GetMapping("/sub_grid")
    public JsonResult querySubGird(BidsQuery query) throws Exception {
        return bidsService.querySubGrid(query);
    }

    /**
     * 发起流程，将子单据推给标书负责人
     */
    @PostMapping("/initiated")
    public JsonResult initiated(@RequestParam String id, @RequestParam String account) throws Exception {
        return bidsService.initiated(id, account);
    }

    @GetMapping("/file_list")
    public JsonResult queryAttachments(@RequestParam @NotBlank String sId, @RequestParam String type) throws Exception {
        return JsonResult.success(bidsService.queryAttachments(sId, type));
    }

    @PostMapping("/file_upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file, @RequestParam String sId,
                             @RequestParam String type) {
        if (file.isEmpty()) {
            return JsonResult.failure("请选择文件");
        }
        String fileName = file.getOriginalFilename();
        assert fileName != null;
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String storageName = RandomUtil.getRandomString(16) + suffix;
        String path = propertyConfig.getAttachment().getPath();

        try {
            File folder = new File(path);
            if (!folder.exists()) {
                folder.mkdir();
            }
            File dest = new File(path + storageName);
            file.transferTo(dest);
            // 如果sId为空，表示新增，不保存到表，返回前端
            if(StringUtils.isBlank(sId)){
                AttachmentPO attachmentPO = new AttachmentPO();
                attachmentPO.setsId(sId);
                attachmentPO.setFileName(fileName);
                attachmentPO.setStorageName(storageName);
                attachmentPO.setStoragePath(path);
                return JsonResult.success(attachmentPO);
            } else {
                AttachmentPO attachmentPO = new AttachmentPO();
                attachmentPO.setsId(sId);
                attachmentPO.setFileName(fileName);
                attachmentPO.setStorageName(storageName);
                attachmentPO.setStoragePath(path);
                bidsService.saveFile(attachmentPO, type);
                return JsonResult.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/file_delete")
    public JsonResult deleteFile(@RequestParam @NotBlank String id) throws Exception {
        return bidsService.deleteFile(id);
    }

    @PostMapping("/result_submit")
    public JsonResult submitResult(@RequestBody @Valid BidsResultDTO bidsResultDTO, @RequestParam String account) throws Exception {
        return bidsService.saveResult(bidsResultDTO, account);
    }

    @PostMapping("/result_confirm")
    public JsonResult confirmResult(@RequestBody @Valid BidsResultDTO bidsResultDTO, @RequestParam String account) throws Exception {
        return bidsService.confirmResult(bidsResultDTO, account);
    }

}
